﻿#pragma once
#include"RBTree.h"
#include<utility>
using std::cout;
using std::endl;
namespace hcc
{
	template<class K, class V>
	class my_map
	{
		struct KeyOfT
		{
			const K& operator()(const std::pair<K, V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename BRTree<K, std::pair<const K, V>, KeyOfT>::iterator iterator;
		typedef typename BRTree<K, const K, KeyOfT>::const_iterator const_iterator;
		iterator begin()
		{
			return _rbt.begin();
		}
		iterator end()
		{
			return _rbt.end();
		}
		const_iterator begin()const
		{
			return _rbt.begin();
		}
		const_iterator end()const
		{
			return _rbt.end();
		}
		std::pair<iterator,bool> insert(std::pair<K, V> data)
		{
			std::pair<iterator, bool> ret=_rbt.Insert(data);
			return ret;
		}
		V& operator[](const K&data)
		{
			std::pair<iterator, bool> ret = _rbt.Insert(data);
			return ret.first->second;
		}
		iterator find(const K& K)
		{
			return _rbt.Find(K);
		}
	private:
		BRTree<K, std::pair<const K, V>, KeyOfT> _rbt;
	};

	void test_map()
	{
		std::map<int, int> mp;
		int arr[] = { 4,6,3,7,2,8,9,10 };
		for (auto e : arr)
		{
			mp.insert({ e,e+5 });
		}

		my_map<std::string, int> m;
		m.insert({ "苹果",1 });
		m.insert({ "香蕉",1 });
		m.insert({ "梨",1 });
		m.insert({ "火龙果",3 });

		my_map<std::string, int>::iterator it = m.begin();
		while (it != m.end())
		{
			//it->first += 'x';
			//it->second += 1;

			//cout << it.operator->()->first << ":" << it->second << endl;
			cout << it->first << ":" << it->second << endl;
			++it;
		}
		cout << "\n\n";
		for (auto e : arr)
		{
			auto ret=mp.find(e);
			cout <<ret->first<<" : "<<ret->second << endl;
		}
			
		cout << endl;
		for (auto e : mp)
			std::cout << e.first << " ";
		cout << endl;
		std::map<int, int>::iterator it2= mp.end();
		while (it2 != mp.begin())
		{
			cout << (--it2)->first <<" ";
		}
		cout << " \n";
	}
};